package com.manor.common.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;

import com.manor.common.CommonAppContext;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;


public class FileUtils {
    private static final String SD_PATH_ = "/sdcard/taomiauth/pic/";
    private static final String IN_PATH_ = "/taomiauth/pic/";

    public static String getPath(Context context, Uri uri) {

        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = {"data"};
            Cursor cursor = null;

            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow("data");
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                // Eat it
            }
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    /**
     * 把字符串保存成文件
     */
    public static void saveStringToFile(File dir, String content, String fileName) {
        PrintWriter writer = null;
        try {
            FileOutputStream os = new FileOutputStream(new File(dir, fileName));
            writer = new PrintWriter(os);
            writer.write(content);
            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }



    public static File ByteToImgFile(byte[] bytes) {
        String filePath;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            filePath = SD_PATH_;
        } else {
            filePath = CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + IN_PATH_;
        }

        filePath += StringUtil.generateFileName();


        File file = new File(filePath);
        BufferedOutputStream stream = null;
        FileOutputStream fstream = null;



        try {
            fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
                if (null != fstream) {
                    fstream.close();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return file;
    }


    public static File byteToImgFile(Context context,byte[] bFile) {
        String fileDest="";
//        if (Environment.getExternalStorageState().equals(
//                Environment.MEDIA_MOUNTED)) {
//            fileDest = SD_PATH_;
//        } else {
//            fileDest = CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + IN_PATH_;
//        }

        fileDest += context.getExternalFilesDir(null).getAbsolutePath()+File.separator+ StringUtil.generateFileName();

        File file ;

        FileOutputStream fos;
        try {
            file = new File(fileDest);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            fos =  new FileOutputStream(file);
            fos.write(bFile);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return file;
    }





}